﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using System.Windows.Data;
using System.Diagnostics;

namespace RStein.PosterousReader.WP.Behaviors
{
  public abstract class TextChangedBehaviorBase<T> : Behavior<T>
                    where T : FrameworkElement
  {
    public TextChangedBehaviorBase()
    {

    }

    

    protected virtual void RunTextChangedAction()
    {
      Action textChangedAction = TextboxPasswordAttachedProperties.GetTextBoxChangedAction(AssociatedObject);
      if (textChangedAction != null)
      {
        textChangedAction();
      }
    }

    protected virtual void UpdateSource()
    {
      var bindingExpression = GetBindingExpression(AssociatedObject);

      if (bindingExpression != null && bindingExpression.ParentBinding.Mode == BindingMode.TwoWay)
      {
        try
        {            
          bindingExpression.UpdateSource();
        }
        catch (Exception e)
        {
          Debug.WriteLine(e);
        }
      }
    }


      public abstract BindingExpression GetBindingExpression(T dpo);
      
    
  }
}
